package team.first.movieapiv2pro.dao.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.springframework.stereotype.Repository;
import team.first.movieapiv2pro.dao.MovieDAO;
import team.first.movieapiv2pro.domain.Movie;
import team.first.movieapiv2pro.util.MongoDBUtil;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2pro.dao.impl
 * @Date: 2021/12/26 10:35
 * @Description:
 */
@Repository
public class MovieDAOImpl implements MovieDAO {
    DBCollection collection = MongoDBUtil.getCollection("movie");

    @Override
    public Movie selectById(Integer movieId) {
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("id", movieId);

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject).sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();
        // 属性考呗
        List<Movie> movieList = BeanUtil.copyToList(dbObjectList, Movie.class);

        if (movieList.size() > 0) {
            return movieList.get(0);
        }
        return null;
    }

    @Override
    public DBCursor selectMovie(BasicDBObject filter_dbobject) {
        return collection.find(filter_dbobject);
    }
}
